#include "video_playback.h"
#include "./ui_video_playback.h"
#include <QGraphicsDropShadowEffect>
#include <QMouseEvent>

Video_playback* Video_playback::instance = nullptr;
Video_playback* Video_playback::getInstance()
{
    if(nullptr == instance)
    {
        instance = new Video_playback;
    }
    return instance;
}
Video_playback::Video_playback(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Video_playback)
{
    ui->setupUi(this);
    initUI();

    connectSignalAndslot();

}

Video_playback::~Video_playback()
{
    delete ui;
}

void Video_playback::onSwitchPage(int pageId)
{
    ui->stackedWidget->setCurrentIndex(pageId);
    resetswitchBtnInfo(pageId);
}

void Video_playback::initUI()
{
    // 去除窗口边框
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    // 设置窗口图标
    setWindowIcon(QIcon(":/images/homePage/logo.png"));

    // 窗口添加阴影效果
    QGraphicsDropShadowEffect* dropShadow = new QGraphicsDropShadowEffect(this);
    dropShadow->setColor(Qt::black);
    dropShadow->setBlurRadius(5);
    dropShadow->setOffset(0, 0);
    ui->bitPlayBg->setGraphicsEffect(dropShadow);

    // 设置页面切换按钮的文本和图片
    ui->homePageBtn->setTextColor("#000000");
    ui->homePageBtn->setImageAndText(":/images/homePage/shouyexuan.png", "主页", HomePage);
    ui->myPageBtn->setTextColor("#999999");
    ui->myPageBtn->setImageAndText(":/images/homePage/wode.png", "我的", MyselfPage);
    ui->sysPageBtn->setTextColor("#999999");
    ui->sysPageBtn->setImageAndText(":/images/homePage/admin.png", "系统", AdminPage);

    // 默认情况下，选中⾸⻚，系统管理⻚⾯隐藏
    ui->stackedWidget->setCurrentIndex(0);
}

void Video_playback::connectSignalAndslot()
{
    connect(ui->minBtn, &QPushButton::clicked, this, &QWidget::showMinimized);
    connect(ui->quitBtn, &QPushButton::clicked, this, &QWidget::close);

    connect(ui->homePageBtn, &PageSwitchButton::switchPage, this, &Video_playback::onSwitchPage);
    connect(ui->myPageBtn, &PageSwitchButton::switchPage, this, &Video_playback::onSwitchPage);
    connect(ui->sysPageBtn, &PageSwitchButton::switchPage, this, &Video_playback::onSwitchPage);
    connect(ui->myPage, &MyselfWidget::switchUploadVideoPage, this, [=](int pageId){
        onSwitchPage(pageId);
    });
    connect(ui->uploadVideoPage, &UploadVideoPage::switchMyselfPage, this, [=](int pageId){
        onSwitchPage(pageId);
    });
}

void Video_playback::resetswitchBtnInfo(int pageId)
{
    // 重新设置左侧body_left中按钮上的文本和图片高亮
    // 设置文本颜色
    QList<PageSwitchButton*> switchBtns = findChildren<PageSwitchButton*>();
    for(auto switchBtn : switchBtns)
    {
        if(switchBtn->getPageId() != pageId)
        {
            switchBtn->setTextColor("#999999");
        }
    }

    // 设置按钮的图片
    if(pageId == HomePage)
    {
        ui->homePageBtn->setImage(":/images/homePage/adminxuan.png");
        ui->myPageBtn->setImage(":/images/homePage/wode.png");
        ui->sysPageBtn->setImage(":/images/homePage/admin.png");
    }
    else if(pageId == MyselfPage)
    {
        ui->homePageBtn->setImage(":/images/homePage/admin.png");
        ui->myPageBtn->setImage(":/images/homePage/wodexuan.png");
        ui->sysPageBtn->setImage(":/images/homePage/admin.png");
    }
    else if(pageId == AdminPage)
    {
        ui->homePageBtn->setImage(":/images/homePage/admin.png");
        ui->myPageBtn->setImage(":/images/homePage/wode.png");
        ui->sysPageBtn->setImage(":/images/homePage/adminxuan.png");
    }
    else
    {
        qDebug() << "暂不支持";
    }
}

void Video_playback::mousePressEvent(QMouseEvent *event)
{
    // 先要获取鼠标点击时的位置
    QPoint point = event->position().toPoint();

    // 检测该位置是否在head中，且鼠标左键单击时
    if(ui->head->geometry().contains(point))
    {
        if(event->button() == Qt::LeftButton)
        {
            dragPos = event->globalPosition().toPoint() - geometry().topLeft();
            return;
        }
    }
    QWidget::mousePressEvent(event);
}

void Video_playback::mouseMoveEvent(QMouseEvent *event)
{
    QPoint point = event->position().toPoint();
    // 改变窗口位置
    // 检测该位置是否在head中，且鼠标左键单击时
    if(ui->head->geometry().contains(point))
    {
        if(event->buttons() == Qt::LeftButton)
        {
            move(event->globalPosition().toPoint() - dragPos);
            return;
        }
    }

}
